capture log close
log using C:\data\UI_2020\logs\regs_b-a_ind-occ_new2.log, replace
*************************************************************************
* Created by: 	Rob Valletta						
* Created on: 	05/19/2022
* Description: 	Before-after regressions by major industry and occupation groups
*		UE regressions only, no state dummies
* Modified on:	02/17/2023 (use updated extract, follow "regs_basic_rev3.do")
*		3/6/23: Import new state unemp/emp data, make sure matched to base month (t-1, not t)
*************************************************************************

version 15
clear *
set more off
capture program drop _all
cd C:\data\UI_2020
set linesize 120

** Pull in analysis extract 
use data\CPS_2020_unbalanced_v2.dta, clear

** (DROP THIS STEP HERE) Reset month to match forward month (through Jul 2020)
** replace month=month+1

* FORM UI REPLACEMENT RATE (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
drop if UI_rep==0
drop if st=="DC"

** DEFINE SOME MACROS
macro define dur_FV i.durmoncat
macro define dur_JR dur dur_sq dur_inv new_un gte26
macro define X_p i.educ i.agecat female married fem_marr i.racegrp
macro define X_s urate* d3lne*

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** Label majind and majocc variables
#d ;
label define ind 1 "Ag" 2 "Mining" 3 "Const" 4 "Manufac" 5 "Whl_Ret" 6 "Trans_util" 7 "Info" 
8 "Financial" 9 "Prof_Bus" 10 "Educ_Health" 11 "Leis_Hosp" 12 "Oth_Serv" 13 "Gov" 14 "Armed_Forces";
#d cr
label values majind ind
label values majind2 ind
label variable majind "Major industry recode"
label variable majind2 "Majind adjusted for lfstat2"

#d ;
label define occ 1 "Manag" 2 "Prof_tech" 3 "Services" 4 "Sales" 5 "Admin" 6 "Farm_Fish"
7 "Const_Extract" 8 "Inst_Maint_Rep" 9 "Production" 10 "Trans_Materials" 11 "Armed_Forces";
#d cr
label values majocc occ
label values majocc2 occ
label variable majocc "Major occupation recode"
label variable majocc2 "Majocc adjusted for lfstat2"

** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

** DROP ARMED FORCES
drop if (majind==14 | majocc==11)

** Drop state vars, create year var for merge
drop emp-d12lne urate*
gen year=2020

** Update and re-match: CURRENT MONTH
  ** (base month for transition)
sort year month state
merge m:1 year month state using data\emp_urate_lf_state, keep(1 3) nogen

** NOW RE-SET MONTH (forward transtions identified by outcome month)
replace month=month+1

** For pre/post design, interact with month group ("post")
gen UI_rep_post=UI_rep*(month>=4)

** Form urate and d3lne cubics
gen urate2=urate^2
gen urate3=urate^3
gen d3lne2=d3lne^2
gen d3lne3=d3lne^3

** REGRESSIONS HERE (2f obs only)
keep if lfstat2==2 & match2f==1

** FIRST DO WITH CATEGORIES EXCLUDED
** OCCUPATIONS FIRST (ex armed forces)
** Set up to extract value labels
local forlab_occ: value label majocc

forvalues i=0/10  {
  local lab_occ:  label `forlab_occ' `i'
** Display results using outreg2
  if `i'==0 {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f
  outreg2 using output\regs_b-a_ex-occ_new2, excel bdec(3) keep(UI_rep*) ctitle("Full sample") replace
  }
  else {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt] if majocc!=`i', vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f_ex_`lab_occ'
  outreg2 using output\regs_b-a_ex-occ_new2, excel bdec(3) keep(UI_rep*) ctitle("ex_`lab_occ'")
  }
}

** INDUSTRIES (ex armed forces)
** Set up to extract value labels
local forlab_ind: value label majind

forvalues j=0/13  {
  local lab_ind:  label `forlab_ind' `j'
** Display results using outreg2
  if `j'==0 {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f
  outreg2 using output\regs_b-a_ex-ind_new2, excel bdec(3) keep(UI_rep*) ctitle("Full sample") replace
  }
  else {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt] if majind!=`j', vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f_ex_`lab_ind'
  outreg2 using output\regs_b-a_ex-ind_new2, excel bdec(3) keep(UI_rep*) ctitle("ex_`lab_ind'")
  }
}

** NOW DO BY CATEGORY

** NEED TO COMBINE SOME CATEGORIES
label drop ind occ

** Industry (combine ag/mining/const, info/fin, prof/bus and gov, leis/hosp and other)
  ** Majind
replace majind=1 if (majind==2 | majind==3)
replace majind=(majind-2) if (majind>3 & majind<8)
replace majind=5 if (majind==8)
replace majind=6 if (majind==9 | majind==13)
replace majind=7 if (majind==10)
replace majind=8 if (majind==11 | majind==12)
  ** Majind2
replace majind2=1 if (majind2==2 | majind2==3)
replace majind2=(majind2-2) if (majind2>3 & majind2<8)
replace majind2=5 if (majind2==8)
replace majind2=6 if (majind2==9 | majind2==13)
replace majind2=7 if (majind2==10)
replace majind2=8 if (majind2==11 | majind2==12)

** Label
#d ;
label define ind 1 "Ag_Min_Const" 2 "Manufac" 3 "Whl_Ret" 4 "Trans_util" 5 "Fin_Info" 
6 "Prof_Bus_Gov" 7 "Educ_Health" 8 "Leis_Hosp_Oth";
#d cr
label values majind ind
label values majind2 ind
label variable majind "Major industry recode"
label variable majind2 "Majind adjusted for lfstat2"

** Occupation (combine farm/const, inst/prod/trans)
  ** Majocc
replace majocc=6 if (majocc==7)
replace majocc=7 if (majocc>7 & majocc<11)
  ** Majocc2
replace majocc2=6 if (majocc2==7)
replace majocc2=7 if (majocc2>7 & majocc2<11)

** Label
#d ;
label define occ 1 "Manag" 2 "Prof_tech" 3 "Services" 4 "Sales" 5 "Admin" 6 "Cons_Farm"
7 "Inst_Prod_Trans";
#d cr
label values majocc occ
label values majocc2 occ
label variable majocc "Major occupation recode"
label variable majocc2 "Majocc adjusted for lfstat2"



** OCCUPATIONS FIRST (ex armed forces)
** Set up to extract value labels
local forlab_occ: value label majocc

forvalues i=0/7  {
  local lab_occ:  label `forlab_occ' `i'
** Display results using outreg2
  if `i'==0 {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s [iw=pwlgwgt], vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f
  outreg2 using output\regs_b-a_occ_new2, excel bdec(3) keep(UI_rep*) ctitle("Full sample") replace
  }
  else {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s [iw=pwlgwgt] if majocc==`i', vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f_`lab_occ'
  outreg2 using output\regs_b-a_occ_new2, excel bdec(3) keep(UI_rep*) ctitle("`lab_occ'")
  }
}

** INDUSTRIES (ex armed forces)
** Set up to extract value labels
local forlab_ind: value label majind

forvalues j=0/8  {
  local lab_ind:  label `forlab_ind' `j'
** Display results using outreg2
  if `j'==0 {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s [iw=pwlgwgt], vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f
  outreg2 using output\regs_b-a_ind_new2, excel bdec(3) keep(UI_rep*) ctitle("Full sample") replace
  }
  else {
  logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s [iw=pwlgwgt] if majind==`j', vce(robust) iterate(20)
  margins, dydx(UI_rep*) post
  eststo UE_2f_`lab_ind'
  outreg2 using output\regs_b-a_ind_new2, excel bdec(3) keep(UI_rep*) ctitle("`lab_ind'")
  }
}


log close
